package com.example.product.dao.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.product.dao.po.Product;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

@Mapper
public interface ProductMapper extends BaseMapper<Product> {
    @Select({
            "<script>",
            "SELECT id FROM product",
            "<where>",
            "<if test='categoryCode != 0'>",
            "id IN (",
            "   SELECT productId FROM product_category",
            "   WHERE category_code = #{categoryCode}",
            "   LIMIT #{page.size} OFFSET #{page.current} * #{page.size} - #{page.size}",
            ")",
            "</if>",
            "</where>",
            "<if test='categoryCode == 0'>",
            "LIMIT #{page.size} OFFSET #{page.current} * #{page.size} - #{page.size}",
            "</if>",
            "</script>"
    })
    Page<Long> selectPaginatedIdsByCategory(Page<Long> page, @Param("categoryCode") Integer categoryCode);
}
